/**
 * Created by CZ on 2017/5/4.
 */
module.exports = function () {
    return{
        restrict: 'AE',
        templateUrl:'views/model/starsModel.html' ,
        scope: {
            ratingValue: '=',
            hoverValue: '=',
            max: '=',
            onHover: '=',
            onLeave: '=',
            readonly: '@'
        },
        controller: require("../controller/Common/StarsCtrl"),
        link: function(scope, elem, attrs) {
            elem.css("display", "inline-block");
            elem.css("text-align", "center");
            var updateStars = function() {
                scope.stars = [];
                // console.log(scope.ratingValue);
                for (var i = 0; i < scope.max; i++) {
                    scope.stars.push({
                        filled: i < scope.ratingValue
                    });
                }
            };
            updateStars();
            var updateStarsHover = function() {
                scope.stars = [];
                for (var i = 0; i < scope.max; i++) {
                    scope.stars.push({
                        filled: i < scope.hoverValue
                    });
                }
            };
            updateStarsHover();
            scope.$watch('ratingValue', function(oldVal, newVal) {
                if (newVal) {
                    updateStars();
                }
            });
            scope.$watch('hoverValue', function(oldVal, newVal) {
                if (newVal) {
                    updateStarsHover();
                }
            });
        }
    }
};